#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QFile>
#include <QTimer>
#include <cstdlib>
#include <ctime>
#include <QDebug>
#include <QMessageBox>
#include<QMovie>
#include<QTextEdit>
#include<QPainter>
#include<mypushbutton.h>
#include<showwinner.h>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //封面
    this->setWindowTitle("抽奖程序");
    this->setWindowIcon(QIcon(":/res/Coin0008.png"));
    ui->pushButton_3->hide();
    this->setFixedSize(this->width(),this->height());
    //动画
    ui->mainAnimation->setStyleSheet("QLabel {background-color: transparent;}");
    ui->mainAnimation->resize(ui->mainAnimation->width(),ui->mainAnimation->height());
    ui->mainAnimation->show();//Label默认显示
    QMovie *mainmovie=new QMovie(":/res/doubleBallSmall.gif");
    ui->mainAnimation->setMovie(mainmovie);
    mainmovie->start();

    //设置退出
    MypushButton *backBtn=new MypushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());
    connect(backBtn,&QPushButton::clicked,[=](){
        //qDebug()<<"点击了button";
        //主场景监听返回按钮
        QTimer::singleShot(300,this,[=](){
            emit this->chooseSceneBack();
        });
    });


    ui->textEdit->setReadOnly(true);
    ui->textEdit->document()->setMaximumBlockCount(1000);
    ui->textEdit_2->document()->setMaximumBlockCount(100);
    ui->textEdit_3->document()->setMaximumBlockCount(100);
    ui->textEdit_4->document()->setMaximumBlockCount(100);
    ui->radioButton->setChecked(true);

    //去除白边框
    QPalette pl=ui->textEdit->palette();
    pl.setBrush(QPalette::Base,QBrush(QColor(255,0,0,50)));
    ui->textEdit->setPalette(pl);
    pl=ui->textEdit_2->palette();
    pl.setBrush(QPalette::Base,QBrush(QColor(255,0,0,30)));
    ui->textEdit_2->setPalette(pl);
    pl=ui->textEdit_3->palette();
    pl.setBrush(QPalette::Base,QBrush(QColor(255,0,0,30)));
    ui->textEdit_3->setPalette(pl);
    pl=ui->textEdit_4->palette();
    pl.setBrush(QPalette::Base,QBrush(QColor(255,0,0,30)));
    ui->textEdit_4->setPalette(pl);


    srand((unsigned)time(NULL));

    //默认名单
    QFile file(QString(":/res/NameList.txt"));
    file.open(QIODevice::ReadOnly);
    QByteArray array=file.readAll();
    ui->textEdit->setText(array);
    file.close();
    file.open(QIODevice::ReadOnly);
    while(!file.atEnd())
    {
        array=file.readLine();
        name[cnt++]=array;
    }

    //载入名单
    connect(ui->pushButton_2,&QPushButton::clicked,[=](){
        QString path = QFileDialog::getOpenFileName(this,"打开文件",":/res/");
        QFile file(path);
        file.open(QIODevice::ReadOnly);
        QByteArray array=file.readAll();
        ui->textEdit->setText(array);
        file.close();
        file.open(QIODevice::ReadOnly);
        while(!file.atEnd())
        {
            array=file.readLine();
            name[cnt++]=array;
        }
        file.close();
    });

    QTimer * timer=new QTimer(this);
    connect(timer,&QTimer::timeout,[=](){
        static int run=0;
        ui->label_3->setText(name[run++]);
        run%=cnt;
    });
    //抽奖事件
    connect(ui->pushButton,&QPushButton::clicked,[=](){
        if(ui->radioButton->isChecked() &&winner3Number>=3){
            QMessageBox::information(this,"Tips","只能有三个三等奖");
        }
        else if(ui->radioButton_2->isChecked() &&winner2Number>=2){
            QMessageBox::information(this,"Tips","只能有两个二等奖");
        }
        else if(ui->radioButton_3->isChecked() &&winner1Number>=1){
            QMessageBox::information(this,"Tips","只能有一个一等奖");
        }
        else if(timer->isActive())
        {
            num=rand()%cnt;
            ui->label_3->setText(name[num]);
            timer->stop();
            if(book[num]==true)
            {
                QMessageBox::information(this,"Tips","这位同学已中过奖");
            }
            else
            {
            book[num]=true;
            if(ui->radioButton->isChecked())
            {
                ui->textEdit_2->append(name[num]);
                winner3Number++;
            }
            else if(ui->radioButton_2->isChecked())
            {
                ui->textEdit_3->append(name[num]);
                winner2Number++;
            }
            else {
                ui->textEdit_4->append(name[num]);
                winner1Number++;
                }
            }
            ui->pushButton->setText("开始");
        }
        else
        {
            timer->start(25);
            ui->pushButton->setText("停止");
        }
        if(winner1Number>=1 &&winner2Number>=2 &&winner3Number>=3){
            ui->pushButton_3->show();
        }
    });

    //查看结果
    connect(ui->pushButton_3,&QPushButton::clicked,this,[=](){
        ShowWinner *showinner=new ShowWinner(ui->textEdit_4->toPlainText(),ui->textEdit_3->toPlainText(),ui->textEdit_2->toPlainText());
        showinner->show();
    });

}


MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *){
    QPixmap BackGroundPic;
    BackGroundPic.load(":/res/MainPicTrans.png");
    BackGroundPic=BackGroundPic.scaled(BackGroundPic.width()*0.9,BackGroundPic.height()*0.9);
    QPainter painter(this);
    painter.drawPixmap(0,0,this->width(),this->height(),BackGroundPic);
};//重写画画函数进行画画
